﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.APITest.Config
{
    public static class APIProjectConfigService
    {
        public static APIProjectConfig ReadConfigFromFile()
        {
            OpenFileDialog open = new OpenFileDialog();
            //open.Filter = ".json";
            open.Multiselect = false;
            if (open.ShowDialog() == true)
            {
                try
                {
                    using (var fileStream = open.File.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read))
                    {
                        APIProjectConfig p = G.SL.Utils.SerializerHelp.JsonDeserialize<APIProjectConfig>(fileStream);
                        return p;
                    }
                }
                catch (FieldAccessException ex)
                {
                    MessageBox.Show("只能读取文档下面的文件！");
                    return null;
                }
            }
            else
                return null;
        }


        public static APIProjectConfig ReadConfigFromIsolate()
        {
            using (var file = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                System.IO.Stream fileStream;
                if (file.FileExists(filePath))
                {
                    fileStream = file.OpenFile(filePath, System.IO.FileMode.Open);
                    return G.SL.Utils.SerializerHelp.JsonDeserialize<APIProjectConfig>(fileStream);
                }
                else
                    return null;
            }
        }

        private const string filePath = "projectconfig.json";
        public static void SaveToIsolate(APIProjectConfig config)
        {
            using (var file = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                //if (file.FileExists(filePath))
                //    file.DeleteFile(filePath);
                //file.OpenFile(filePath, System.IO.FileMode.CreateNew);
                var fileStream = file.OpenFile("aaaaaa/projectconfig.json", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
                G.SL.Utils.SerializerHelp.JsonSerializer(config, fileStream);
                fileStream.Close();
            }
        }

        public static void SaveAs(APIProjectConfig config)
        {
            SaveToIsolate(config);
            SaveFileDialog save = new SaveFileDialog();
            if (save.ShowDialog() == true)
            {
                using (var savefile = save.OpenFile())
                {
                    G.SL.Utils.SerializerHelp.JsonSerializer(config, savefile);
                    savefile.Close();
                }
            }
        }

        public static APIProjectConfig CreateNewProject(string projectName)
        {
            return new APIProjectConfig() { ProjectName = projectName };
        }
    }
}
